//
//  ViewController.m
//  Refresh
//
//  Created by weitayinqing on 15/9/23.
//  Copyright © 2015年 苏昌雄. All rights reserved.
//

#import "ViewController.h"
#import "UIScrollView+RCKRefreshControl.h"
#import "RickyRefreshView.h"

//
//
//

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
    UITableView *table;
    NSMutableArray *dataArray;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"RickyRefreshDemo";
    
    dataArray = @[].mutableCopy;
    
    table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    //    table.estimatedRowHeight = 60;
    table.rowHeight = 60;
    [self.view addSubview:table];
    
    __weak __typeof(table) weakTable = table;
    [table addNormalRickyRefreshHeaderViewWithCallBackAction:^{
        __strong __typeof(weakTable) strongTable = weakTable;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [strongTable completeRickyRefreshHeaderWithCompleteType:RickyRefreshCompleteTypeSuccess];
        });
    }];
    
    /* 新添加api函数，设置加载页面完成后，就开始刷新机制 */
    [table setHeaderRefreshAtBegin:YES];
    
    [table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellIdentifier"];
    
    [self handleTableData:^{
        [table reloadData];
    }];
}

- (void)handleTableData:(void(^)())then
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [dataArray addObject:@"RikcyNormalRefreshDemo"];
        [dataArray addObject:@"RickyCoreTextRefreshDemo"];
        [dataArray addObject:@"RickyGifRefreshDemo"];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            !then ? :then();
        });
    });
}

#pragma mark -- table'delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    }
    cell.textLabel.text = dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *vcName = indexPath.row==0?@"NormalViewController":indexPath.row==1?@"CoreTextViewController":@"GifViewController";
    Class cls = NSClassFromString(vcName);
    UIViewController *viewController = [[cls alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
